﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.SiteAdmin.Entity;

namespace Ivy.SiteAdmin.DataAccess
{
    public class ContactRequestRegisterDao :IDaoInterface<ContactRequestRegister, ContactRequestRegisterCriteria>
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #region IDaoInterface<ContactRequestRegister, ContactRequestRegisterCriteria> Members
        public bool Insert(ContactRequestRegister ContactRequestRegisterEntity)
        {
            bool IsOperationSuccessful = false;
            
                if (ContactRequestRegisterEntity.EntityState == EntityState.New)
                {
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Dasideen_ContactRequestRegister_Insert";
                    CmdObj.CommandType = CommandType.StoredProcedure;
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContactRequesterName", ParameterDirection.Input, DbType.String, ContactRequestRegisterEntity.ContactRequesterName);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContactRequesterEmail", ParameterDirection.Input, DbType.String, ContactRequestRegisterEntity.ContactRequesterEmail);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContactRequestMessage", ParameterDirection.Input, DbType.String, ContactRequestRegisterEntity.ContactRequestMessage);


                    DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, ContactRequestRegisterEntity);

                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContactRequestKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);
                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    int _NoOfRecord = CmdObj.ExecuteNonQuery();
                    if (_NoOfRecord == 1)
                    {
                        ContactRequestRegisterEntity.ContactRequestKey = Convert.ToInt32(CmdObj.Parameters["@ContactRequestKey"].Value);
                        ContactRequestRegisterEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                        ContactRequestRegisterEntity.Sys_DateOfModification = ContactRequestRegisterEntity.Sys_DateOfCreation;
                        ContactRequestRegisterEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }
                    CmdObj.Dispose();
                    DbConn.Close();
                }
                else
                {

                    throw new Exception("For Inserting new ContactRequestRegister entity data to database,[EntityState] attribute should be [New]");
                }
            
            return IsOperationSuccessful;
        }

        public bool Update(ContactRequestRegister ContactRequestRegisterEntity)
        {
            bool IsOperationSuccessful = false;
            
                if (ContactRequestRegisterEntity.EntityState == EntityState.Changed)
                {
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Dasideen_ContactRequestRegister_Update";
                    CmdObj.CommandType = CommandType.StoredProcedure;

                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContactRequestKey", ParameterDirection.Input, DbType.Int32, ContactRequestRegisterEntity.ContactRequestKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContactRequesterName", ParameterDirection.Input, DbType.String, ContactRequestRegisterEntity.ContactRequesterName);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContactRequesterEmail", ParameterDirection.Input, DbType.String, ContactRequestRegisterEntity.ContactRequesterEmail);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContactRequestMessage", ParameterDirection.Input, DbType.String, ContactRequestRegisterEntity.ContactRequestMessage);

                    DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, ContactRequestRegisterEntity);

                    DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    int _NoOfRecords = CmdObj.ExecuteNonQuery();
                    if (_NoOfRecords == 1)
                    {
                        ContactRequestRegisterEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                        ContactRequestRegisterEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }

                    CmdObj.Dispose();
                    DbConn.Close();
                }
                else
                {
                    throw new Exception("For Updating existing ContactRequestRegister entity data to database, [EntityState] attribute should be [Changed]");
                }
            
            return IsOperationSuccessful;
        }

        public bool Delete(ContactRequestRegister DataEntity)
        {
            throw new NotImplementedException();
        }

        public ContactRequestRegister GetByID(string ID)
        {
            throw new NotImplementedException();
        }

        public List<ContactRequestRegister> GetAll()
        {
            List<ContactRequestRegister> lContactRequestRegisterList = new List<ContactRequestRegister>();

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ContactRequestRegister_GetAll";
            CmdObj.CommandType = CommandType.StoredProcedure;

            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();

            DbDataReader _DataReader = CmdObj.ExecuteReader();
            if (_DataReader.HasRows)
            {
                while (_DataReader.Read())
                {
                    ContactRequestRegister _TempContactRequestRegister = new ContactRequestRegister(); //IMPORTANT NOTE:- Create instance only when required.
                    this.TranslateSys_ErrorLogFieldsFromReaderToEntity(_TempContactRequestRegister, _DataReader);
                    lContactRequestRegisterList.Add(_TempContactRequestRegister);
                }
            }
            CmdObj.Dispose();
            DbConn.Close();


            return lContactRequestRegisterList;
        }

        public List<ContactRequestRegister> GetByCriteria(ContactRequestRegisterCriteria CriteriaEntity)
        {
            throw new NotImplementedException();
        }
        #endregion

        public ContactRequestRegister GetByContactRequestKey(int ContactRequestKey)
        {
            {
                ContactRequestRegister _ContactRequestRegisterList = new ContactRequestRegister(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
                
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Dasideen_ContactRequestRegister_GetByContactRequestKey";
                    CmdObj.CommandType = CommandType.StoredProcedure;

                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContactRequestKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ContactRequestKey));


                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    DbDataReader _DataReader = CmdObj.ExecuteReader();

                    if (_DataReader.HasRows)
                    {
                        if (_DataReader.Read())
                        {
                            _ContactRequestRegisterList = new ContactRequestRegister(); //IMPORTANT NOTE:- Create instance only when required.
                            TranslateContactRequestRegisterFieldsFromReaderToEntity(_ContactRequestRegisterList, _DataReader);
                        }
                    }
                    else
                    {
                        throw new Exception("No ContactRequestRegister found");
                    }
                    CmdObj.Dispose();
                    DbConn.Close();
                
                return _ContactRequestRegisterList;
            }
        }

        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private void TranslateSys_ErrorLogFieldsFromReaderToEntity(ContactRequestRegister pContactRequestRegister, DbDataReader pDbDataReader)
        {

            pContactRequestRegister.ContactRequestKey = Convert.ToInt32(pDbDataReader["ContactRequestKey"]);
            pContactRequestRegister.ContactRequesterName = Convert.ToString(pDbDataReader["ContactRequesterName"]);
            pContactRequestRegister.ContactRequesterEmail = Convert.ToString(pDbDataReader["ContactRequesterEmail"]);
            pContactRequestRegister.ContactRequestMessage = Convert.ToString(pDbDataReader["ContactRequestMessage"]);

            DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pContactRequestRegister, pDbDataReader);

        }

        private void TranslateContactRequestRegisterFieldsFromReaderToEntity(ContactRequestRegister pContactRequestRegister, DbDataReader pDbDataReader)
        {
            
                pContactRequestRegister.ContactRequestKey = Convert.ToInt32(pDbDataReader["ContactRequestKey"]);
                pContactRequestRegister.ContactRequesterName = Convert.ToString(pDbDataReader["ContactRequesterName"]);
                pContactRequestRegister.ContactRequesterEmail = Convert.ToString(pDbDataReader["ContactRequesterEmail"]);
                pContactRequestRegister.ContactRequestMessage = Convert.ToString(pDbDataReader["ContactRequestMessage"]);

                DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pContactRequestRegister, pDbDataReader);
            
        }
        #endregion Private Methods

    }
}
